package com.fare.taluo.application.impl.plane.flight;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fare.taluo.core.domain.config.product.ProductEntity;
import com.fare.taluo.vo.core.ProductEntityVO;
import com.fare.taluo.application.plane.flight.ProductManageApplication;

@Transactional
@Scope("singleton")
@Service("ProductManageApplication")
public class ProductManageApplicationImpl implements ProductManageApplication{

	@Override
	public List<ProductEntityVO> findAll() {
		ProductEntity productEntity = new ProductEntity();
		List<ProductEntity> list = productEntity.findAll();
		List<ProductEntityVO> voList = new ArrayList<ProductEntityVO>();
		for (ProductEntity entity : list) {
				ProductEntityVO vo  = ProductEntityTOProductEntityVO(entity);
				voList.add(vo);
		}
		return voList;
	}

	@Override
	public List<ProductEntityVO> findProductByConditions(String name,String companyName,
			String startCity,String endCity,String productType) {
		ProductEntity productEntity = new ProductEntity();
		List<ProductEntity> list = productEntity.findProductByConditions(name, companyName,startCity,endCity,productType);
		List<ProductEntityVO> voList = new ArrayList<ProductEntityVO>();
		for (ProductEntity entity : list) {
			ProductEntityVO vo  = ProductEntityTOProductEntityVO(entity);
			voList.add(vo);
		}
		return voList;
	}

	@Override
	public void saveOrUpdate(ProductEntityVO productEntityVO) {
		ProductEntity productEntity = new ProductEntity();
		productEntity.setCompanyName(productEntityVO.getCompanyName());
		productEntity.setId(new Long(productEntityVO.getId()));
		productEntity.setName(productEntityVO.getName());
		productEntity.setPrice(productEntityVO.getPrice());
		productEntity.setProductLink("<a href=\""+productEntityVO.getProductLink()+"\" target=\"_blank\">"+productEntityVO.getProductLink()+"</a>");
		productEntity.setProductType(productEntityVO.getProductType());
		productEntity.setStartCity(productEntityVO.getStartCity());
		productEntity.setEndCity(productEntityVO.getEndCity());
		productEntity.setSpace(productEntityVO.getSpace());
		productEntity.setTagetDate(productEntityVO.getTagetDate());
		productEntity.setTravelDate(productEntityVO.getTravelDate());
		productEntity.setVoyage(productEntityVO.getVoyage());
		productEntity.saveOrUpdate(productEntity);
	}

	@Override
	public void delete(int id) {
		ProductEntity productEntity = new ProductEntity();
			productEntity.setId(new Long(id));
			productEntity.delete(productEntity);
	}
	
	public ProductEntityVO ProductEntityTOProductEntityVO(ProductEntity entity){
		ProductEntityVO vo  = new ProductEntityVO();
		vo.setCompanyName(entity.getCompanyName());
		vo.setId(entity.getId().intValue());
		vo.setName(entity.getName());
		vo.setPrice(entity.getPrice());
		vo.setProductLink(entity.getProductLink());
		vo.setProductType(entity.getProductType());
		vo.setStartCity(entity.getStartCity());
		vo.setEndCity(entity.getEndCity());
		vo.setSpace(entity.getSpace());
		vo.setTagetDate(entity.getTagetDate());
		vo.setTravelDate(entity.getTravelDate());
		vo.setVoyage(entity.getVoyage());
		return vo;
	} 

}
